package com.giraffeb.editor;


import java.io.File;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.giraffeb.editor.model.EditorModel;


@Controller
@RequestMapping(value="/daum")
public class EditorController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	@Inject
	@Named("editorModel")
	private EditorModel editorModel;
	
	@RequestMapping(value="/first")
	public String getEditor(HttpServletRequest request) throws Exception{
		
		System.out.println("Come in daum.editor");
		System.out.println(request.getContextPath());
		return "daumeditor/editor";
	}
	
	
	@RequestMapping(value="/second")
	public ModelAndView getList() throws Exception{
		
		logger.info("Editor getList()...");
		
		
		return new ModelAndView("list","list",editorModel.getList());
	}
	
	/*@RequestMapping(value="third")
	public ModelAndView getParams(@RequestParam Map<String,Object> params) throws Exception{
		
		editorModel.addContent(params);
		return new ModelAndView("list","list", params);
	}*/
	@RequestMapping(value="third")
	public ModelAndView getParams(
			@RequestParam Map<String,Object> params
			,HttpServletRequest request
			,HttpServletResponse response) throws Exception{
		
		request.setCharacterEncoding("UTF-8");
		
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		List items = upload.parseRequest(request);
		
		FileItem item1 = (FileItem)items.get(0);
		
		
		
		File file = new File("c:\\down\\"+item1.getName());
		item1.write(file);
		
		
		
		return new ModelAndView("list","list", params);
	}
}
